#ifndef GLCANVAS_H
#define GLCANVAS_H

#include <QGLWidget>
#include <QVector>
#include <QPoint>
#include <QMouseEvent>
#include <QMenu>
#include <QAction>
#include <QImage>
#include <QRgb>
#include <QKeySequence>
#include <QFileDialog>

#include <glu.h>
//#include <glut.h>

#include "../definitions.h"

class GLCanvas : public QGLWidget
{

        Q_OBJECT


        enum DRAWMODE
        {
            POINTS,
            WIREFRAME,
            SOLID_FLAT,
            SOLID_SMOOTH,
            HIDDEN_LINE,
            COLORED_VERTICES,
            FLAT_COLORED_FACES,
            SMOOTH_COLORED_FACES,
            TEXTURE_1D,
            TEXTURE_2D
        };

        typedef enum DRAWMODE DrawMode;



        public:

                GLCanvas(QWidget *parent = 0);

                void saveSnapshot();

                void saveSilhouette();

                void saveAugmentedSilhouette();

                void setDefaultMaterial(void);

                void setCustomMaterial(void);

                void setDefaultLight(void);

                void translate(OpenMesh::Vec3f trans);

                void rotate(OpenMesh::Vec3f axis, float angle);

                void exportToPovRay();

                void applyTexture(QString filename);

                void applyQualityRamp1DTexture();




        public
        slots:

                void updateMeshList(QVector<MyMesh*> *mesh);

                void setModePOINTS()                { drawMode = POINTS;                updateGL(); }
                void setModeWIREFRAME()             { drawMode = WIREFRAME;             updateGL(); }
                void setModeSOLID_FLAT()            { drawMode = SOLID_FLAT;            updateGL(); }
                void setModeSOLID_SMOOTH()          { drawMode = SOLID_SMOOTH;          updateGL(); }
                void setModeHIDDEN_LINE()           { drawMode = HIDDEN_LINE;           updateGL(); }
                void setModeCOLORED_VERTICES()      { drawMode = COLORED_VERTICES;      updateGL(); }
                void setModeFLAT_COLORED_FACES()    { drawMode = FLAT_COLORED_FACES;    updateGL(); }
                void setModeSMOOTH_COLORED_FACES()  { drawMode = SMOOTH_COLORED_FACES;  updateGL(); }
                void setModeTEXTURE_1D()            { drawMode = TEXTURE_1D;            updateGL(); }
                void setModeTEXTURE_2D()            { drawMode = TEXTURE_2D;            updateGL(); }



        protected:

                void initializeGL();

                void resizeGL(int w, int h);

                void paintGL();

                void updateProjectionMatrix(void);

                void centerScene(void);

                bool mapToSphere(QPoint v2D, OpenMesh::Vec3f& v3D);

                void mousePressEvent(QMouseEvent *event);

                void mouseReleaseEvent(QMouseEvent*);

                void mouseMoveEvent(QMouseEvent *event);

                void wheelEvent(QWheelEvent *event);

                void drawScene(DrawMode mode);

                void drawMesh(DrawMode mode);

                void createPopupMenu();

                void generate2DTexture(QString filename);

                void keyPressEvent(QKeyEvent *event);

                void exportGeometryToPovRay(QString path, MyMesh *m);



        private:

                // View parameters
                OpenMesh::Vec3f center;
                float           radius;


                GLdouble projection_matrix[16],
                         modelview_matrix[16];


                GLuint textureID;


                QPoint           lastPoint2D;
                OpenMesh::Vec3f  lastPoint3D;
                bool             lastPointOk;


                DrawMode         drawMode;
                QMenu           *popupMenu;


                QVector<MyMesh*> *mesh;

};

#endif // GLCANVAS_H
